#include "main.h"
#include "delay.h"
#include "user_usart.h"
#include "user_tim.h"
#include "user_gpio.h"
#include "ti_msp_dl_config.h"
#include "menu_fsm.h"
#include "pid_app.h"
#include "timeout.h"
#include "mission_1_fsm.h"
#include "mission_2_fsm.h"
#include "mission_3_fsm.h"
#include "./OLED/OLED.h"
#include "./imu/imu.h"
#include "./encorder/encorder.h"
#include "./tlsf/tlsf_porting.h"
#include "./lw_queue/lw_queue_porting.h"
#include "./lw_timer/lw_timer.h"
#include "./lw_timer/lw_timer_callback.h"
#include "./letter_shell/letter_shell_porting.h"
#include "./flexible_button/flexbutton_porting.h"

/********************静态变量定义********************/
static param_event_t read_event;


int main(void)
{
	/********************系统级初始化********************/
	SYSCFG_DL_init(); /*初始化系统资源*/		
	
	/********************硬件级初始化********************/
	OLED_Init();
	imu_init();
	
	/********************框架基层初始化********************/
	memory_pool_init(); /*初始化内存池*/
	lwq_hook_set(); /*设置队列内存钩子*/
	
	/********************框架组件初始化********************/
	debug_shell_init(); /*初始化letter shell*/		
	lw_timer_set_stamp(sys_tick_get); /*载入软件定时器时基*/
	
	/********************用户级初始化********************/
	user_button_init(); /*初始化用户按键*/
	autocar_pid_init(); /*初始化pid*/
	menu_fsm_init(); /*初始化菜单状态机*/
	mission_1_fsm_init(); /*初始化任务1状态机*/
	mission_2_fsm_init(); /*初始化任务2状态机*/
	mission_3_fsm_init(); /*初始化任务3状态机*/
	
	btn_scan_tim_start(); /*启动按键扫描软件定时器*/
	oled_update_tim_start(); /*启动OLED刷新任务软件定时器*/
	letter_shell_tim_start(); /*启动letter shell任务软件定时器*/
	pid_ctrl_tim_start(); /*启动pid控制软件定时器*/
	imu_scan_tim_start();/*启动角度读取定时器*/

	/********************正式开启用户中断********************/
	debug_usart_rec_start(); /*开启调试接收*/	
	motor_speed_measure_int_enable(); /*使能电机测速中断*/
	
	while(1)
	{
		if(lwq_read(&menu_fsm_que, &read_event))
		{
			menu_fsm_dispatch(&menu_fsm, &read_event);
		}		
		
		if(lwq_read(&mission_1_fsm_que, &read_event))
		{
			mission_1_fsm_dispatch(&mission_1_fsm, &read_event);
		}
		
		if(lwq_read(&mission_2_fsm_que, &read_event))
		{
			mission_2_fsm_dispatch(&mission_2_fsm, &read_event);
		}
		
		if(lwq_read(&mission_3_fsm_que, &read_event))
		{
			mission_3_fsm_dispatch(&mission_3_fsm, &read_event);
		}
		
		lw_timer_scheduler(); /*执行软件定时器任务*/	
		timeout_event_manage(); /*超时事件管理器*/
	}
}
